
package org.openeai.lite.console.beans;

import java.io.Serializable;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.log4j.Logger;
import org.openeai.lite.common.control.BrokerController;
import org.openeai.lite.common.exception.OpenEAIException;
import org.openeai.lite.common.model.Queue;
import org.openeai.lite.common.model.Topic;
import org.openeai.lite.common.util.Constants;


@ManagedBean(name = "brokerBean")
@SessionScoped
public class BrokerBean implements Serializable
{

    private static Logger log = Logger.getLogger(BrokerBean.class);

    private static final long serialVersionUID = 1L;

    // Properties
    private boolean started;

    private String status = Constants.INACTIVE;

    private String brokername;

    private String brokerid;

    private String startdate;

    private Integer queues = 0;

    private Integer topics = 0;

    private Integer temporaryQueues = 0;

    private Integer temporaryTopics = 0;

    private ArrayList<Queue> queueList;

    private ArrayList<Topic> topicList;

    private Long memoryUsage;

    private String owurl;

    private String configuration;

    // booleans for rendering status. By default, these views are NOT rendered.
    private boolean queueView = false;

    private boolean topicView = false;

    private boolean newQueueView = false;

    private boolean newTopicView = false;

    private boolean sendView = false;


    // Started?
    public boolean isStarted()
    {
        try
        {
            started = BrokerController.getInstance().isStarted();
        }
        catch (OpenEAIException oue)
        {
            // Logging and exception handling should be done in controller
            ;
        }
        return started;
    }

    public void setStarted(boolean started)
    {
        this.started = started;
    }

    // Status?
    public String getStatus()
    {
        try
        {
            if (BrokerController.getInstance().isStarted())
            {
                this.status = Constants.ACTIVE;
            }
            else
            {
                this.status = Constants.INACTIVE;
            }
        }
        catch (OpenEAIException oue)
        {
            // Logging and exception handling should be done in controller
            ;
        }

        return status;
    }


    public void setStatus(String status)
    {
        if (status.equals(Constants.ACTIVE))
        {
            this.status = Constants.ACTIVE;
        }
        else if (status.equals(Constants.INACTIVE))
        {
            this.status = Constants.INACTIVE;
        }
        else
        {
            // dunno
            ;
        }
    }

    // Queues
    public Integer getQueues()
    {
        try
        {
            this.queues = BrokerController.getInstance().getQueueCount();
        }
        catch (OpenEAIException oue)
        {
            // Something went wrong getting connections from activemq. 
            // Down perhaps?
            this.queues = 0;
        }
        return queues;
    }


    // QueueList
    public ArrayList<Queue> getQueueList()
    {
        try
        {
            this.queueList = BrokerController.getInstance().getQueues();
        }
        catch (OpenEAIException oue)
        {
            // Something went wrong getting connections from activemq. 
            // Down perhaps?
            this.queueList = null;
        }
        return queueList;
    }


    // Topics
    public Integer getTopics()
    {
        try
        {
            this.topics = BrokerController.getInstance().getTopicCount();
        }
        catch (OpenEAIException oue)
        {
            // Something went wrong getting connections from activemq. 
            // Down perhaps?
            this.topics = 0;
        }
        return topics;
    }


    // TopicList
    public ArrayList<Topic> getTopicList()
    {
        try
        {
            this.topicList = BrokerController.getInstance().getTopics();
        }
        catch (OpenEAIException oue)
        {
            // Something went wrong getting connections from activemq. 
            // Down perhaps?
            this.topicList = null;
        }
        return topicList;
    }


    // Temporary Queues
    public Integer getTemporaryQueues()
    {
        try
        {
            this.temporaryQueues = BrokerController.getInstance().getTemporaryQueueCount();
        }
        catch (OpenEAIException oue)
        {
            // Something went wrong getting connections from activemq. 
            // Down perhaps?
            this.temporaryQueues = 0;
        }
        return temporaryQueues;
    }
    


    // Temporary Topics
    public Integer getTemporaryTopics()
    {
        try
        {
            this.temporaryTopics = BrokerController.getInstance().getTemproraryTopicCount();
        }
        catch (OpenEAIException oue)
        {
            // Something went wrong getting connections from activemq. 
            // Down perhaps?
            this.temporaryTopics = 0;
        }
        return temporaryTopics;
    }



    // Start date
    public String getStartdate()
    {
        return startdate;
    }

    public void setStartdate(String startdate)
    {
        this.startdate = startdate;
    }

    // Broker ID
    public String getBrokerid()
    {
        try
        {
            this.brokerid = BrokerController.getInstance().getBrokerId();
        }
        catch (Exception e)
        {
            log.error("Error getting BrokerID from ActiveMQ brokerservice.");
            log.error(e.getCause());
            this.brokerid = "UNKNOWN";
        }
        return brokerid;
    }


    // Broker Name
    public String getBrokername()
    {
        try
        {
            this.brokername = BrokerController.getInstance().getBrokerName();
        }
        catch (Exception e)
        {
            log.error("Error getting BrokerName from ActiveMQ brokerservice.");
            log.error(e.getCause());
            this.brokername = Constants.UNKNOWN;
        }
        return brokername;
    }


    // MemoryUsage
    public Long getMemoryUsage()
    {
        try
        {
            this.memoryUsage = BrokerController.getInstance().getMemoryUsage();
        }
        catch (OpenEAIException e)
        {
            this.memoryUsage = new Long(0);
        }
        return memoryUsage;
    }

    // OpenWire URL
    public String getOwurl()
    {
        try
        {
            this.owurl = BrokerController.getInstance().getOpenWireURL();
        }
        catch (OpenEAIException e)
        {
            this.owurl = Constants.UNKNOWN;
        }
        return owurl;
    }

    public String getConfiguration()
    {
        try
        {
            this.configuration = BrokerController.getInstance().getConfiguration();
        }
        catch (OpenEAIException e)
        {
            this.configuration = Constants.UNKNOWN;
        }
        return configuration;
    }


    // queuesView active?
    public boolean isQueueView()
    {
        return queueView;
    }

    public void toggleQueueView()
    {
        this.queueView = !queueView;
    }

    // topicsView active?
    public boolean isTopicView()
    {
        return topicView;
    }

    public void toggleTopicView()
    {
        this.topicView = !topicView;
    }

    // newQueueView active?
    public boolean isNewQueueView()
    {
        return newQueueView;
    }

    public void toggleNewQueueView()
    {
        this.newQueueView = !newQueueView;
    }

    // newTopicView active?
    public boolean isNewTopicView()
    {
        return newTopicView;
    }

    public void toggleNewTopicView()
    {
        this.newTopicView = !newTopicView;
    }

    // sendView active?
    public boolean isSendView()
    {
        return sendView;
    }

    public void toggleSendView()
    {
        this.sendView = !sendView;
    }

 
}
